/**
 * 
 */
package in.ac.iitb.cse.nlp.postagger.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

/**
 * @author zerone
 * 
 */
public abstract class TableCreator {
	static public final String WORD_SEPARATOR = " ";
	static public final String TAG_SEPARATOR = "_";
	// We will divide the input into sentences so that
	// we can process it faster
	static public final String START_OF_SENTENCE = "^";
	static public final String END_OF_SENTENCE = ".";
	static public final String ALTERNATE_END_OF_SENTENCE = "?";
	// For new table creation
	static public TransitionMatrix transitionMatrix = new TransitionMatrix();
	static public EmissionMatrix emissionMatrix = new EmissionMatrix();
	static public Set<String> tagSet = new HashSet<String>();

	// String previousTag = START_OF_SENTENCE;
	static public String prevTag = null;

	/**
	 * 
	 */
	public TableCreator() {
	}

	public void processLine(String line) {

	}

	public void createNew(File file) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(file)));
		String line = null;
		prevTag = null;
		while ((line = reader.readLine()) != null) {
			processLine(line);
		}

	}

	public void newDisplayTables() {
		System.out.println(transitionMatrix.toString());
		System.out.println(emissionMatrix.toString());
		System.out.println(tagSet);
		System.out.println(tagSet.size());
	}

	public void goFile(String filename) {
		if (filename.endsWith("txt") == false)
			return;
		System.out.println(filename + " is Trained");
		File file = new File(filename);
		try {
			createNew(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void goDir(String cdir, String dir) {
		if (cdir.endsWith("/") == false && dir.endsWith("/") == false)
			cdir.concat("/");
		File Dir = new File(cdir + dir);
		if (Dir.isDirectory()) {
			for (File f : Dir.listFiles()) {
				// Dont assume that file trained good...
				// return the exit Status, check it
				if (f.isFile() && f.canRead()) {
					goFile(f.getAbsolutePath());
				}
			}

		} else if (Dir.canRead() && Dir.isFile()) {
			// Dont assume that file trained good...
			// return the exit Status, check it
			goFile(Dir.getAbsolutePath());
		}
	}

}
